package maze;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GamePanel extends JPanel implements ActionListener {
    int n=30, width = 600, padding = 20;
    Maze p;
    public GamePanel() {
        p = new Maze(n, (width - padding - padding) / n, padding);
        createFrame();
    }
    private void createFrame() {
        JFrame frame = new JFrame("迷宫游戏");
        frame.getContentPane().add(p);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(width + padding + 100, width + padding + padding + 100 );
        frame.setLocation(900, 400);
        frame.setVisible(true);
        createMenu(frame);
    }

    private Font createFont() {
        return new Font("思源宋体", Font.BOLD, 26);
    }

    private void createMenu(JFrame frame) {
        Font tFont = createFont();
        JMenuBar jmb = new JMenuBar();

        JMenuItem jMenu1 = new JMenuItem("重新生成");
        jMenu1.setFont(tFont);
        JMenuItem jMenu2 = new JMenuItem("帮助");
        jMenu2.setFont(tFont);
        JMenuItem jMenu3 = new JMenuItem("退出");
        jMenu3.setFont(tFont);
        JMenuItem jMenu4 = new JMenuItem("提示");
        jMenu4.setFont(tFont);
        JMenu jMenu=new JMenu("模式");
        jMenu.setFont(tFont);
        JMenuItem i1=new JMenuItem("简单");
        i1.setFont(tFont);
        JMenuItem i2=new JMenuItem("普通");
        i2.setFont(tFont);
        JMenuItem i3=new JMenuItem("困难");
        i3.setFont(tFont);

        jMenu.add(i1);
        jMenu.add(i2);
        jMenu.add(i3);
        jmb.add(jMenu1);
        jmb.add(jMenu2);
        jmb.add(jMenu4);
        jmb.add(jMenu3);
        jmb.add(jMenu);
        frame.setJMenuBar(jmb);
        jMenu1.addActionListener(this);
        jMenu2.addActionListener(this);
        jMenu3.addActionListener(this);
        jMenu4.addActionListener(this);
        i1.addActionListener(this);
        i2.addActionListener(this);
        i3.addActionListener(this);
        jMenu1.setActionCommand("restart");
        jMenu3.setActionCommand("exit");
        jMenu4.setActionCommand("find");
        jMenu2.setActionCommand("help");
        i1.setActionCommand("easy");
        i2.setActionCommand("normal");
        i3.setActionCommand("hard");

    }
    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        System.out.println(command);
        UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("思源宋体", Font.ITALIC, 18)));
        UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("思源宋体", Font.ITALIC, 18)));
        if ("exit".equals(command)) {
            Object[] options = {"确定", "取消"};
            int response = JOptionPane.showOptionDialog(this, "您确认要退出吗", "",
                    JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                    options, options[0]);
            if (response == 0) {
                System.exit(0);
            }
        } else if ("restart".equals(command)) {
            restart();
        } else if ("help".equals(command)) {
            help();
        } else if ("find".equals(command)) {
            JOptionPane.showMessageDialog(null, "通过上，下，左，右，控制小球移动",
                    "提示！", JOptionPane.INFORMATION_MESSAGE);
        }
        else if (command.equals("easy")){
            this.n=10;
        }
        else if (command.equals("normal")){
            this.n=20;
        }
        else if (command.equals("hard")){
            this.n=30;
        }
    }
    void restart() {
        p.into();
    }
    void help(){
        p.setDrawPath(true);
    }
}